package com.hxf.dao.role.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dao.BaseDaoImpl;
import com.hxf.dao.role.RoleDao;
import com.hxf.dto.MenuDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.RoleDto;
import com.hxf.dto.RoleMenuDto; 
import com.hxf.dto.AdminUserDto;
import com.hxf.exception.DaoLevelException; 

/**
 * DAO角色实现类
 * @author tengyicheng
 * @version [1.0, 2013/09/23]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository("roleDao")
public class RoleDaoImpl extends BaseDaoImpl<RoleDto> implements RoleDao {

	@Override
	public ReturnData<RoleDto> findByRole(RoleDto roleDto, int targetPage, int pageSize) throws DaoLevelException {
		String sql = "";
		if (PublicUtil.isNotNull(roleDto) && PublicUtil.isNotNullAndEmpty(roleDto.getRoleName())) {
			sql = " and roleName like '" + roleDto.getRoleName().trim() + "%'";
		}
		return this.findBy("from RoleDto WHERE 1=1" + sql, targetPage, pageSize); 
	}

	@Override
	public List<RoleDto> findByRole(RoleDto roleDto) throws DaoLevelException {
		String sql = "";
		if (PublicUtil.isNotNull(roleDto) && PublicUtil.isNotNullAndEmpty(roleDto.getRoleName())) {
			sql = " and roleName like '" + roleDto.getRoleName() + "%'";
		}
		return this.findBy("from RoleDto WHERE status=0" + sql); 
	}
	
	@Override
	public List<MenuDto> findByRoleMenu(RoleMenuDto roleMenuDto) throws DaoLevelException {
		 String sql  = " select rdm.menu_id,rdm.menu_name,rdm.parent_id from rk_role rr,rk_dic_menu rdm,rk_role_menu rrm  where rr.role_id = rrm.role_id and rdm.menu_id = rrm.menu_id and rr.role_id=" + roleMenuDto.getRoleId();
		 List list = this.exeSQLList(sql);
		 List<MenuDto> userMenuVoList = null;
			if (list != null && list.size() > 0) {
				userMenuVoList = new ArrayList<MenuDto>();
				for (int i = 0;i < list.size(); i++) {
					Object [] o = (Object[]) list.get(i);
					MenuDto menu = new MenuDto();
				 	menu.setMenuId(null==o[0]?null:Integer.parseInt(o[0].toString()));
					menu.setMenuName(null==o[1]?null:o[1].toString());
					menu.setParentId(null==o[2]?null:Integer.parseInt(o[2].toString()));
					userMenuVoList.add(menu);
				}
			}
			return userMenuVoList; 
	}

	@Override
	public int deleteRoleMenuByRoleId(Integer roleId) throws DaoLevelException {
		return this.executeUpdateBySql("delete from rk_role_menu where role_id="+ roleId);
	}
	
	@Override
	public ReturnData<RoleDto> findByUserRole(RoleDto roleDto, int targetPage, int pageSize) throws DaoLevelException{
		//select distinct rr.role_id ,rr.role_name ,rr.remark,rr.status 
		//String hql = " from rk_user as ru, rk_admin_role as rur,rk_role as rr where ru.user_id = rur.user_id "+ 
		// "and rur.role_id = rr.role_id  and ru.user_id="+userId+"";  distinct rr.roldId,rr.roleName,rr.remark,rr.status
		String sqlRoleName = "";
		if(null != roleDto.getRoleName() && !"null".equals(roleDto.getRoleName())){
			sqlRoleName = " and rr.role_name =  + '"+roleDto.getRoleName().trim()+"' ";
		}
		
		String sql = "select rr.role_id,rr.create_time,rr.create_user,rr.role_name,rr.remark,rr.status " +
				     "from rk_user as ru, rk_admin_role as rur,rk_role as rr where ru.user_id = rur.user_id "+ 
				     "and rur.role_id = rr.role_id  and ru.user_id="+roleDto.getRoleId()+" " + sqlRoleName; 
		ReturnData<RoleDto> list = this.exeSQLList(sql, targetPage, pageSize);
		RoleDto role = null;
		List listRole = new ArrayList();
		listRole = list.getResultlist();
		List<RoleDto> listRoleDto = new ArrayList<RoleDto>();
		
		if(null != listRole && listRole.size()>0){
			for(int i=0;i<listRole.size();i++){
				role = new RoleDto();
				Object[] o  =  (Object[])listRole.get(i);
				role.setRoleId(null==o[0]?null:Integer.valueOf(o[0].toString()));
				role.setCreateTime(null==o[1]?null:o[1].toString());
				role.setCreateUser(null==o[2]?null:o[2].toString());
				role.setRoleName(null==o[3]?null:o[3].toString());
				role.setRemark(null==o[4]?null:o[4].toString());
				role.setStatus(null==o[5]?null:Integer.valueOf(o[5].toString()));
				listRoleDto.add(role);
			}
		}
		
		list.setResultlist(listRoleDto);
		return list;
	}
}
